﻿using System;
using System.Net;
using System.Windows.Controls;
using Hi_Baidu.Entry.CollectionObject;
using System.Text;

namespace Hi_Baidu.ProcessModel
{
    public class UpdateProcessModel : Model
    {
        private static String PackageBody;
        private static byte[] protocol;

        /// <summary>
        ///     向服务器发送修改个性签名请求
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="NewUserProd" type="string">
        ///     <para>
        ///         新的个性签名
        ///     </para>
        /// </param>
        public static void ModifyProd(byte[] username, byte[] NewUserProd)
        {
            if (username.Length > 0 && NewUserProd.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] ProdLength = BitConverter.GetBytes(NewUserProd.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + None.Length + IPAddress.Length + NewUserProd.Length + 16];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //个性签名长度
                Array.ConstrainedCopy(ProdLength, 0, PackageBody, Offset, ProdLength.Length);
                Offset += 4;
                //个性签名
                Array.ConstrainedCopy(NewUserProd, 0, PackageBody, Offset, NewUserProd.Length);
                Offset += NewUserProd.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("ModifyProd",
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.
                                                                           MODIFY_PROD_PACKAGE,
                                                                       (int) Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     将挂起的新用户信息更新到服务器上
        /// </summary>
        /// <param name="win_ModifyUserInfo" type="Hi_Baidu.Win_ModifyUserInfo">
        ///     <para>
        ///         主面板句柄
        ///     </para>
        /// </param>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="DB_FILED" type="string">
        ///     <para>
        ///         数据库关联字段
        ///     </para>
        /// </param>
        /// <param name="NEW_VALUE" type="string">
        ///     <para>
        ///         修改后的新值
        ///     </para>
        /// </param>
        public static void UpdateUserChange(Win_ModifyUserInfo win_ModifyUserInfo, byte[] username, byte[] DB_FILED, byte[] NEW_VALUE)
        {
            if (username.Length > 0 && DB_FILED.Length > 0 && NEW_VALUE.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] DBFiledLength = BitConverter.GetBytes(DB_FILED.Length);
                byte[] NewValueLength = BitConverter.GetBytes(NEW_VALUE.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + NEW_VALUE.Length + DB_FILED.Length + None.Length + IPAddress.Length + 20];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //关联字段长度
                Array.ConstrainedCopy(DBFiledLength, 0, PackageBody, Offset, DBFiledLength.Length);
                Offset += 4;
                //关联字段
                Array.ConstrainedCopy(DB_FILED, 0, PackageBody, Offset, DB_FILED.Length);
                Offset += DB_FILED.Length;
                //新值长度
                Array.ConstrainedCopy(NewValueLength, 0, PackageBody, Offset, NewValueLength.Length);
                Offset += 4;
                //新值
                Array.ConstrainedCopy(NEW_VALUE, 0, PackageBody, Offset, NEW_VALUE.Length);
                Offset += NEW_VALUE.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("UpdateChanage" + Encoding.Default.GetString(DB_FILED),
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.
                                                                           UPDATE_USER_CHANAGE,
                                                                       (int) Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器发送更改用户分组请求包
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="item">更改的分组</param>
        /// <param name="NewValue">更新的值</param>
        public static void UpdateItem(byte[] username, TreeViewItem item, byte[] NewValue)
        {
            if (username.Length > 0 && item != null)
            {
                byte[] ItemId = BitConverter.GetBytes(int.Parse((item.Tag as Item).Key));
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] NewValueLength = BitConverter.GetBytes(NewValue.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + NewValue.Length + None.Length + IPAddress.Length + 20];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //分组ID
                Array.ConstrainedCopy(ItemId, 0, PackageBody, Offset, ItemId.Length);
                Offset += 4;
                //新值长度
                Array.ConstrainedCopy(NewValueLength, 0, PackageBody, Offset, NewValueLength.Length);
                Offset += 4;
                //新值
                Array.ConstrainedCopy(NewValue, 0, PackageBody, Offset, NewValue.Length);
                Offset += NewValue.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("UpdateItem",
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.
                                                                           UPDATE_ITEM,
                                                                       (int)Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器发送添加新分组请求包
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="ItemName">新分组名称</param>
        /// <param name="ItemKey">新分组KEY</param>
        public static void AddItem(byte[] username, byte[] ItemName, byte[] ItemKey)
        {
            if (username.Length > 0 && ItemName.Length > 0)
            {
                byte[] ItemKeyLength = BitConverter.GetBytes(ItemKey.Length);
                byte[] ItemNameLength = BitConverter.GetBytes(ItemName.Length);
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + ItemName.Length + ItemKey.Length + None.Length + IPAddress.Length + 20];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //分组名称长度
                Array.ConstrainedCopy(ItemNameLength, 0, PackageBody, Offset, ItemNameLength.Length);
                Offset += 4;
                //分组名称
                Array.ConstrainedCopy(ItemName, 0, PackageBody, Offset, ItemName.Length);
                Offset += ItemName.Length;
                //分组KEY长度
                Array.ConstrainedCopy(ItemKeyLength, 0, PackageBody, Offset, ItemKeyLength.Length);
                Offset += 4;
                //分组KEY
                Array.ConstrainedCopy(ItemKey, 0, PackageBody, Offset, ItemKey.Length);
                Offset += ItemKey.Length;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("AddItem",
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.ADD_NEW_ITEM,
                                                                       (int) Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }

        /// <summary>
        ///     向服务器发送删除用户分组请求包
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="ItemId">分组ID</param>
        public static void DeleteItem(byte[] username, byte[] ItemId)
        {
            if (username.Length > 0 && ItemId.Length > 0)
            {
                byte[] UsernameLength = BitConverter.GetBytes(username.Length);
                byte[] IPAddress = Encoding.Default.GetBytes(Dns.GetHostAddresses(Dns.GetHostName())[0].ToString());
                byte[] IPLength = BitConverter.GetBytes(IPAddress.Length);
                byte[] TalkPort = BitConverter.GetBytes(Globle.user.TALK_PORT);
                byte[] None = new byte[2];
                byte[] PackageBody = new byte[username.Length + None.Length + IPAddress.Length + 16];
                Offset = 0;
                //用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, PackageBody, Offset, UsernameLength.Length);
                Offset += 4;
                //用户名
                Array.ConstrainedCopy(username, 0, PackageBody, Offset, username.Length);
                Offset += username.Length;
                //分组ID长度
                Array.ConstrainedCopy(ItemId, 0, PackageBody, Offset, ItemId.Length);
                Offset += 4;
                //空白 
                Array.ConstrainedCopy(None, 0, PackageBody, Offset, None.Length);
                Offset += 2;
                //IP地址长度
                Array.ConstrainedCopy(IPLength, 0, PackageBody, Offset, IPLength.Length);
                Offset += 4;
                //IP地址
                Array.ConstrainedCopy(IPAddress, 0, PackageBody, Offset, IPAddress.Length);
                Offset += IPAddress.Length;
                //聊天端口
                Array.ConstrainedCopy(TalkPort, 0, PackageBody, Offset, TalkPort.Length);
                protocol = ProtocolFactory.ProtocolFactory.GetProtocol("DeleteItem",
                                                                       (int)
                                                                       Packet.Protocol.OUT_NetworkCommand.DELETE_ITEM,
                                                                       (int)Packet.Protocol.NetworkProtocol.Base, PackageBody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       Globle.user.SESSION_CODE);
                //发送请求
                NetControl.Send(protocol, 8700, ServerIp);
            }
        }
    }
}
